/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Emanoel
 */
public class ArquivoUtil {

    public static int contaLinhas(String arquivo) {
        BufferedReader br = null;
        int i = 0;
        try {
            br = new BufferedReader(new FileReader(arquivo));
            while (br.readLine() != null) {
                i++;
            }
        } catch (IOException ex) {
            Logger.getLogger(ArquivoUtil.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                br.close();
            } catch (IOException ex) {
                Logger.getLogger(ArquivoUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return i;
    }

    public static double[][] converteBase(String arquivo) {
        BufferedReader br = null;
        double[][] retorno = new double[contaLinhas(arquivo)][];
        int ordemLinha = 0;
        try {
            br = new BufferedReader(new FileReader(arquivo));
            String linha = br.readLine();
            String[] valores = null;
            while (linha != null) {
                valores = linha.split(",");
                retorno[ordemLinha] = new double[valores.length - 1];
                for (int i = 0; i < valores.length - 1; i++) {
                    retorno[ordemLinha][i] = Double.parseDouble(valores[i]);
                }
                ordemLinha++;
                linha = br.readLine();
            }
        } catch (IOException ex) {
            Logger.getLogger(ArquivoUtil.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                br.close();
            } catch (IOException ex) {
                Logger.getLogger(ArquivoUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return retorno;
    }

    public static HashMap<String, String> obterClasses(String arquivo) {
        BufferedReader br = null;
        HashMap<String, String> retorno = new HashMap<String, String>();
        int ordemLinha = 0;
        try {
            br = new BufferedReader(new FileReader(arquivo));
            String linha = br.readLine();
            String[] valores = null;
            int virgula = 0;
            while (linha != null) {
                virgula = linha.lastIndexOf(",");
                retorno.put(linha.substring(0, virgula), linha.substring(virgula + 1));
                linha = br.readLine();
            }
        } catch (IOException ex) {
            Logger.getLogger(ArquivoUtil.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                br.close();
            } catch (IOException ex) {
                Logger.getLogger(ArquivoUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return retorno;
    }
    
    public static void adicionarLog(String texto){
        BufferedWriter bw = null;
        try {
            FileOutputStream file = new FileOutputStream("src/rede/log.txt",true);
            DataOutputStream out   = new DataOutputStream(file);
            out.writeBytes(texto);
            out.flush();
            out.close();
            
//            bw = new BufferedWriter(new FileWriter("src/rede/log.txt"));
//            char[] chars = texto.toCharArray();
//            for (int i = 0; i < chars.length; i++) {
//                char c = chars[i];
//                bw.append(c);
//            }
//            bw.close();
        } catch (IOException ex) {
            Logger.getLogger(ArquivoUtil.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            
        }
    }

    public static String converter(double[] valores) {
        String retorno = "";
        for (int i = 0; i < valores.length; i++) {
            double d = valores[i];
            retorno += d + ",";
        }
        return retorno.substring(0, retorno.length() - 1);
    }
}
